﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace PrinterMonitoringByGPCL
{
    public partial class FormPrinterMonitoringByGPCL : Form
    {
        public FormPrinterMonitoringByGPCL()
        {
            InitializeComponent();
        }

        private string SPLToString(string fileName)
        {
            IntPtr inptr = IntPtr.Zero;
            IntPtr callInptr = IntPtr.Zero;
            int result = GPCLClass.gsapi_new_instance(ref inptr, out callInptr);
            if (result == 0)
            {
                try
                {
                    int argc = 7;
                    string[] argv = new string[argc];
                    argv[0] = "";
                    argv[1] = "-dSAFER";
                    argv[2] = "-dBATCH";
                    argv[3] = "-dNOPAUSE";
                    argv[4] = "-sDEVICE=tiff24nc";
                    argv[5] = $"-sOutputFile={System.AppDomain.CurrentDomain.BaseDirectory}/Image/%d.tiff";
                    argv[6] = fileName;
                    result = GPCLClass.gsapi_init_with_args(inptr, argc, argv);
                    if (result == 0)
                    {
                        string[] imageFile = Directory.GetFiles($"{System.AppDomain.CurrentDomain.BaseDirectory}Image", "*.tiff");
                        if (imageFile.Length == 0)
                        {
                            return "转换失败，result=" + result;
                        }
                        else
                        {
                            string text = "";
                            for (int i = 0; i < imageFile.Length; i++)
                            {
                                Bitmap pic = new Bitmap(imageFile[i]);
                                Console.WriteLine($"Printing page {i},page width={pic.Size.Width},page height={pic.Size.Height}");
                                text += $"Printing page {i},page width={pic.Size.Width},page height={pic.Size.Height}\r\n";
                                pic.Dispose();
                                File.Delete(imageFile[i]);
                            }
                            return text;
                        }
                    }
                    else
                    {
                        return "转换失败，result=" + result;
                    }
                }
                catch (Exception ex)
                {
                    return ex.ToString();
                }
                finally
                {
                    GPCLClass.gsapi_exit(inptr);
                    GPCLClass.gsapi_delete_instance(inptr);
                }
            }
            else
            {
                return "初始化失败";
            }
            return "解析失败";
        }

        #region 控件事件
        private void textBox1_DragDrop(object sender, DragEventArgs e)
        {
            string fileName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            textBox1.Text = SPLToString(fileName);
        }

        private void textBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Link;
            else e.Effect = DragDropEffects.None;
        }
        #endregion
    }
}